﻿using IDAdmin.Common;
using IDAdmin.Lib.DataLayer;
using IDAdmin.Lib.UI;
using IDAdmin.Lib.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace IDAdmin.Pages
{
    public partial class StatsCodeAction : BasePage
    {
        public StatsCodeAction()
            : base(Lib.AppFunctions.STATSCODEACTION)
        {

        }
        protected override void Page_Load(object sender, EventArgs e)
        {
            if (!IsLogin())
            {
                RedirectToLogOn();
            }
            else if (!CheckRight())
            {
                RedirectToDeniedMessage();
            }
        }

        protected void countButton_Click(object sender, EventArgs e)
        {
            DateTime dtFormDate = new DateTime();
            DateTime dtToDate = new DateTime();
            if (!string.IsNullOrEmpty(countToolBar.beginDateTextBox.Text))
            {
                dtFormDate = IDAdmin.Common.ConverterWorkData.convertStringToDateTime(countToolBar.beginDateTextBox.Text);
            }
            if (!string.IsNullOrEmpty(countToolBar.beginDateTextBox.Text))
            {
                dtToDate = IDAdmin.Common.ConverterWorkData.convertStringToDateTime(countToolBar.toDateTextBox.Text);
            }
            List<DateTime> dateTimeList = new List<DateTime>();
            if (!string.IsNullOrEmpty(Convert.ToString(dtFormDate)) && !string.IsNullOrEmpty(Convert.ToString(dtToDate)))
            {
                dateTimeList = IDAdmin.Common.ConverterWorkData.getDateListBetween(dtFormDate, dtToDate);
            }            
            dataPanel.Controls.Add(getStatsActionCode(WebDB_N.getPCardData(dateTimeList)));
        }

        private Table getStatsActionCode(List<pCard> data)
        {
            Table table = new Table();
            table.CssClass = "table1";
            TableRow rowHeader = new TableRow();
            rowHeader.Cells.AddRange(
                new TableCell[]
                { 
                    UIHelpers.CreateTableCell("Thời gian",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Tổng số code",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),   
                     UIHelpers.CreateTableCell("code Tân Thủ",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"), 
                      UIHelpers.CreateTableCell("code Giang Hồ",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"), 
                       UIHelpers.CreateTableCell("code Hào Kiệt",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader")
                }
            );
            table.Rows.Add(rowHeader);
            
            try
            {


                Dictionary<DateTime, List<pCard>> lData = new Dictionary<DateTime, List<pCard>>();
                int j = 0;
                for (int i = 0; i < data.Count(); i++)
                {
                    DateTime day = data.ElementAt(i).usetime;
                    List<pCard> lValue = new List<pCard>();
                    while (j < data.Count())
                    {
                        if (data.ElementAt(j).usetime == day)
                        {
                            lValue.Add(data.ElementAt(j));                            
                            j++;                           
                            i = j;
                        }
                        else
                        {                            
                            i = j - 1;
                            break;
                        }
                      
                    }
                    lData.Add(day, lValue);
                }
                string css;
                int stt = 0;
                try
                {
                    foreach (var item in lData)
                    {
                        stt++;
                        css = stt % 2 == 0 ? "cell1" : "cell2";
                        TableRow row = new TableRow();
                       //    if (item.Value.Count == 3)
                      //  {
                        int countCode1 = item.Value.Count >= 1 && item.Value.ElementAt(0).typeid == "1" ? item.Value.ElementAt(0).count : 0;
                        int countCode2 = item.Value.Count >= 2 && item.Value.ElementAt(1).typeid == "2" ? item.Value.ElementAt(1).count : 0;
                        int countCode3 = item.Value.Count >= 3 && item.Value.ElementAt(2).typeid == "3" ? item.Value.ElementAt(2).count : 0;
                        int total = countCode1 + countCode2 + countCode3;
                            row.Cells.AddRange
                            (
                                new TableCell[]
                                    {
                                        UIHelpers.CreateTableCell(item.Key.ToString(),HorizontalAlign.Center,css),
                                        UIHelpers.CreateTableCell(total.ToString() ,HorizontalAlign.Center,css),
                                        UIHelpers.CreateTableCell(countCode1.ToString() ,HorizontalAlign.Center,css),
                                        UIHelpers.CreateTableCell(countCode2.ToString(),HorizontalAlign.Center,css),     
                                        UIHelpers.CreateTableCell(countCode3.ToString(),HorizontalAlign.Center,css)          
                                    }
                            );
                            table.Rows.Add(row);
                       // }
                        //else if (item.Value.Count == 2)
                        //{
                        //    row.Cells.AddRange
                        //   (
                        //       new TableCell[]
                        //            {
                        //                UIHelpers.CreateTableCell(item.Key.ToString(),HorizontalAlign.Center,css),
                        //                UIHelpers.CreateTableCell( (Convert.ToInt32(item.Value.ElementAt(0).ToString()) + Convert.ToInt32(item.Value.ElementAt(1).ToString())).ToString() ,HorizontalAlign.Center,css),
                        //                UIHelpers.CreateTableCell((item.Value.ElementAt(0) == null && item.Value.ElementAt(0) == string.Empty) ? "0" : item.Value.ElementAt(0) ,HorizontalAlign.Center,css),
                        //                UIHelpers.CreateTableCell((item.Value.ElementAt(1) == null && item.Value.ElementAt(1) == string.Empty) ? "0" : item.Value.ElementAt(1),HorizontalAlign.Center,css),                                            
                        //            }
                        //   );
                        //    table.Rows.Add(row);
                        //}
                        //else
                        //{
                        //    row.Cells.AddRange
                        //     (
                        //         new TableCell[]
                        //                {
                        //                    UIHelpers.CreateTableCell(item.Key.ToString(),HorizontalAlign.Center,css),
                        //                    UIHelpers.CreateTableCell( item.Value.ElementAt(0) ,HorizontalAlign.Center,css),
                        //                    UIHelpers.CreateTableCell((item.Value.ElementAt(0) == null && item.Value.ElementAt(0) == string.Empty) ? "0" : item.Value.ElementAt(0) ,HorizontalAlign.Center,css)                                          
                        //                }
                        //     );
                         //   table.Rows.Add(row);
                       // }
                       
                    }
                }catch(Exception ex){
                   
                }             

            }
            catch (Exception ex)
            {

            }
            return table;
        }

    }
}